package com.guotd.ticketSystem.controller;

import com.guotd.ticketSystem.entity.Cabin;
import com.guotd.ticketSystem.mapper.CabinMapper;
import com.guotd.ticketSystem.mapper.FlightMapper;
import com.guotd.ticketSystem.utils.SessionUtil;
import org.apache.ibatis.session.SqlSession;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/editCabin")
public class EditCabinServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        String id = request.getParameter("id");
        String name = request.getParameter("name");
        String food = request.getParameter("food");
        String service = request.getParameter("service");
        String hand_baggage = request.getParameter("hand_baggage");
        String checked_baggage = request.getParameter("checked_baggage");
        double price = Double.parseDouble(request.getParameter("price"));
        int capacity = Integer.parseInt(request.getParameter("capacity"));
        int booked= Integer.parseInt(request.getParameter("booked"));
        String flightId = request.getParameter("flightId");
        Cabin cabin = new Cabin(id,name,food,service,hand_baggage,checked_baggage,price,capacity,booked,flightId);
        try {
            SqlSession session = SessionUtil.getSqlSession();
            CabinMapper cabinMapper = session.getMapper(CabinMapper.class);
            int row = cabinMapper.updateCabin(cabin);
            session.commit();
            session.close();
            if (row > 0) {
                request.getSession().setAttribute("cabin_msg", "修改成功！");
                request.getRequestDispatcher("getCabins").forward(request, response);
            }
        } catch (Exception e) {
            request.getSession().setAttribute("cabin_msg", "修改失败！");
            request.getRequestDispatcher("getCabins").forward(request, response);
        }
    }
}
